package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmUserDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.model.wemedia.vos.WmUserVo;
import com.heima.utils.common.AppJwtUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.HashMap;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {
    /**
     * 查询自媒体人
     * @param name
     * @return
     */
    @Override
    public ResponseResult<WmUser> findWmUserByName(String name) {
        //参数校验
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //查询用户
        WmUser wmUser = this.getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        //封装结果返回

        return ResponseResult.okResult(wmUser);
    }

    /**
     * 新增保存用户
     * @param wmUser
     * @return
     */
    @Override
    public ResponseResult<WmUser> saveWmUser(WmUser wmUser) {
        //校验参数
        if (wmUser == null || StringUtils.isBlank(wmUser.getName()) || StringUtils.isBlank(wmUser.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //保存用户
        this.save(wmUser);
        return ResponseResult.okResult(wmUser);
    }

    /**
     * 用户登录
     * @param wmUserDto
     * @return
     */
    @Override
    public ResponseResult login(WmUserDto wmUserDto) {
        //校验参数
        if (StringUtils.isBlank(wmUserDto.getName()) || StringUtils.isBlank(wmUserDto.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //根据用户名去数据库查询
        ResponseResult<WmUser> wmUserByName = this.findWmUserByName(wmUserDto.getName());
        WmUser user = wmUserByName.getData();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "查无此人");
        }
        String salt = user.getSalt();
        String pwd = wmUserDto.getPassword();
        String password = DigestUtils.md5DigestAsHex((pwd + salt).getBytes());
        if(!user.getPassword().equals(password)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"密码错误");
        }
        //颁发令牌
        Integer id = user.getId();
        String token = AppJwtUtil.getToken(user.getId().longValue());
        WmUserVo wmUserVo = new WmUserVo();
        BeanUtils.copyProperties(user,wmUserVo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",wmUserVo);
        return ResponseResult.okResult(map);


    }

}
